package org.caenv.interpreter.pojo.instructions.comparator;

import java.util.Map;
import org.caenv.interpreter.ExecutionException;
import org.caenv.interpreter.pojo.instructions.ExecutionPlan;
import org.caenv.interpreter.pojo.instructions.Instruction;
import org.caenv.interpreter.pojo.instructions.Scope;

public abstract class Compare extends Scope {

    @Override
    public final boolean build(Map<String, String> attributes) {
        return attributes == null ? true : attributes.isEmpty();
    }

    @Override
    public final boolean execute(ExecutionPlan executionPlan) throws ExecutionException {

        Comparable value1 = (Comparable) executionPlan.getMemoryManager().getValue(executionPlan.getComparator().getValue1());
        Comparable value2 = (Comparable) executionPlan.getMemoryManager().getValue(executionPlan.getComparator().getValue1());

        if (testValues(value1, value2)) {

            for (Instruction instruction : super.getState().getInstructions()) {
                instruction.execute(executionPlan);
            }

        }

        return true;

    }

    protected abstract boolean testValues(Comparable value1, Comparable value2);

}
